package com.itheima.exerciseTest.test20_29;
/*
3.分析以下需求，并用代码实现：
	(1)定义List集合，存入多个字符串
	(2)删除集合中包含0-9数字的字符串(只要字符串中包含0-9中的任意一个数字就需要删除此整个字符串)
	(3)然后利用迭代器遍历集合元素并输出
 */


import java.util.*;
import java.util.function.Predicate;

public class Test29 {
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        // (1)定义List集合，存入多个字符串
        System.out.println("请输入字符串的最长长度：");
        int length = sc.nextInt();
        System.out.println("请输入产生多少个随机字符：");
        int number = sc.nextInt();
        List<String> list = setList(length, number);
        // 删除前
        prinList(list);
        System.out.println("\n=============================================================");

        // (2)删除集合中包含0-9数字的字符串(只要字符串中包含0-9中的任意一个数字就需要删除此整个字符串)
        // removeNumber(list);
        removeNumberIf(list);

        // (3)然后利用迭代器遍历集合元素并输出
        prinList(list);

    }

    // 获取集合
    private static List<String> setList(int length, int number) {
        Random r = new Random();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            String str = "";
            for (int j = 0; j < r.nextInt(length + 1); j++) {
                int num;
                while (true) {
                    num = r.nextInt(75) + 48;
                    if (num >= '0' && num <= '9') break;
                    else if (num >= 'A' && num <= 'Z') break;
                    else if (num >= 'a' && num <= 'z') break;
                }
                str += (char) num;
            }
            list.add(str);
        }
        return list;
    }

    // 删除集合中含数字的字符串 remove
    public static void removeNumber(List<String> list) {
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String next = it.next();
            char[] chars = next.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] >= '0' && chars[i] <= '9') {
                    it.remove();
                    break;
                }
            }
        }
    }

    // 删除集合中含数字的字符串 removeIf
    public static void removeNumberIf(List<String> list){
        list.removeIf(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                char[] chars = s.toCharArray();
                for (int i = 0; i < chars.length; i++) {
                    if(chars[i] >= '0' && chars[i] <= '9'){
                        return true;
                    }
                }
                return false;
            }
        });
    }

    // 打印集合
    private static void prinList(List<String> list) {
        for (String s : list)
            System.out.print(s + " ");
    }


}
